/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pos.ui.beans;

import com.pos.domain.Item;
import com.pos.domain.ItemCategory;
import com.pos.services.BaseService;
import com.pos.ui.jsf.util.JsfUtil;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author prina
 */
@Named("productBean")
@RequestScoped
public class ProductBean implements Serializable {

    @Inject
    private transient BaseService baseService;
    private Item product;
    private boolean editMode;

    public BaseService getBaseService() {
        return baseService;
    }

    public void setBaseService(BaseService baseService) {
        this.baseService = baseService;
    }

    public Item getProduct() {
        return product;
    }

    public void createNewProduct() {
        this.product = new Item();
        setEditMode(true);
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public boolean isEditMode() {
        return this.editMode;
    }

    public void setProduct(Item product) {
        this.product = product;
    }

    public List<Item> getProducts() {
        return baseService.findAllProduct();
    }

    public SelectItem[] getCategories() {
        return JsfUtil.getSelectItems(baseService.findAllCatagory(), false);
    }

    public void edit(Object id) {
        this.product = baseService.findProductById(id);
        setEditMode(true);
    }

    public void save() {
        baseService.saveProduct(product);
        setEditMode(false);
    }

    @PostConstruct
    public void reset() {
        this.product = new Item();
    }

    public ItemCategory findCategory(Object id) {
        return baseService.findCategoryById(id);
    }

    public Item findItem(Object id) {
        return baseService.findProductById(id);
    }
}
